package com.servlet;

import com.dao.EmployeesDao;
import com.dao.impl.EmployeesDaoImpl;
import com.entity.Employees;
import com.google.gson.Gson;
import com.vo.ResponseData;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/*
 *
 *@author XCH
 *@date2025 - 02 - 24
 *
 */
@WebServlet("/api/employees/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String phone = req.getParameter("phone");
        String sta_pos = req.getParameter("sta_pos");
        EmployeesDao employeesDao = new EmployeesDaoImpl();
        Employees employees = new Employees();
        employees.setName(name);
        employees.setAge(Integer.parseInt(age));
        employees.setPhone(phone);
        employees.setSta_pos(sta_pos);
        System.out.println(employees);
        int row = employeesDao.insert(employees);
        System.out.println(row);
        ResponseData responseData = new ResponseData();
        if (row == 1) {
            responseData.setCode(200);
            responseData.setMsg("success");
            responseData.setData(null);
        } else {
            responseData.setCode(500);
            responseData.setMsg("error");
            responseData.setData(null);
        }
        String json = new Gson().toJson(responseData);
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
        out.close();
    }
}
